package com.now.printer.ui.activity;

import android.content.Context;
import android.content.res.Configuration;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.now.printer.R;
import com.now.printer.base.BaseActivity2;
import com.now.printer.bean.TabEntity;
import com.now.printer.ui.fragment.HomeFragment;
import com.now.printer.ui.fragment.MoreContainerFragment;
import com.now.printer.ui.fragment.MyContainerFragment;
import com.now.printer.utils.ResUtils;
import com.xuexiang.xutil.common.ClickUtils;
import com.xuexiang.xutil.tip.ToastUtils;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;

public class MainActivity extends BaseActivity2 implements ClickUtils.OnClick2ExitListener {
    private CommonTabLayout mTabLayout;

    private int[] mIconSelectIds = {R.mipmap.tab1,
            R.mipmap.tab2
          ,R.drawable.icon_tabbar_component_selected
    };

    private int[] mIconUnselectIds = {
            R.mipmap.untab1, R.mipmap.untab2
          ,R.drawable.icon_tabbar_component
    };

    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void doBusiness(Context mContext) {
//        mTabLayout = (TabLayout) findViewById(R.id.tabs);
        initTab();
        initSlidingMenu(savedInstanceState);
    }

    private String[] mainType;
    private ViewPager vp;
    private ArrayList<CustomTabEntity> tabEntitys = new ArrayList<>();

    private void initTab() {
        mTabLayout = (CommonTabLayout) findViewById(R.id.tabs);
        vp = (ViewPager) findViewById(R.id.vp);
        ArrayList<Fragment> fragments = new ArrayList<>();
        mainType = ResUtils.getStringArray(R.array.main_type);
        for (int i = 0; i < mainType.length; i++) {
            tabEntitys.add(new TabEntity(mainType[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        HomeFragment homeFragment = new HomeFragment();
        homeFragment.setOnTabListener(new HomeFragment.OnTabListener() {
            @Override
            public void onSetTab() {
                mTabLayout.setCurrentTab(1);
            }
        });
        fragments.add(homeFragment);
        fragments.add(new MyContainerFragment());
        fragments.add(new MoreContainerFragment());
        mTabLayout.setTextSelectColor(this.getResources().getColor(R.color.color_2C97DE));
        mTabLayout.setTextUnselectColor(this.getResources().getColor(R.color.color_ADADAD));

        mTabLayout.setTabData(tabEntitys, this, R.id.fl_change, fragments);

//        mTabLayout.getIconView(2).setMaxWidth(DensityUtil.px2dip(34));
//        mTabLayout.getIconView(2).setMaxHeight(DensityUtil.px2dip(34));
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mTabLayout.setCurrentTab(position);

            }

            @Override
            public void onTabReselect(int position) {
                //TODO 重选
            }
        });
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        initSlidingMenu(null);
    }

    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isMenuOpen()) {
                closeMenu();
            } else {
                ClickUtils.exitBy2Click(2000, MainActivity.this);
            }
        }
        return true;
    }

    /**
     * 再点击一次
     */
    @Override
    public void onRetry() {
        ToastUtils.toast("再按一次退出程序");
    }

    /**
     * 退出
     */
    @Override
    public void onExit() {
        moveTaskToBack(true);
    }

    //    /**
//     * 初始化Tab
//     */
//    private void initTab() {
//        component = mTabLayout.newTab();
//        component.setText("首页");
//        component.setIcon(SettingSPUtils.getInstance().isUseCustomTheme() ? R.drawable.custom_selector_icon_tabbar_component : R.drawable.selector_icon_tabbar_component);
//        mTabLayout.addTab(component);
//
//        util = mTabLayout.newTab();
//        util.setText("我的");
//        util.setIcon(SettingSPUtils.getInstance().isUseCustomTheme() ? R.drawable.custom_selector_icon_tabbar_util : R.drawable.selector_icon_tabbar_util);
//        mTabLayout.addTab(util);
//
//        expand = mTabLayout.newTab();
//        expand.setText("更多");
//        expand.setIcon(SettingSPUtils.getInstance().isUseCustomTheme() ? R.drawable.custom_selector_icon_tabbar_expand : R.drawable.selector_icon_tabbar_expand);
//        mTabLayout.addTab(expand);
//
////        WidgetUtils.setTabLayoutTextFont(mTabLayout);
//
//        mTabLayout.setupWithViewPager(component);
//        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                mAdapter.setSelected(tab.getPosition());
//                switch (tab.getPosition()) {
//                    case 0:
//                        mTabLayout.selectTab(component);
//                        break;
//                    case 1:
//                        mTabLayout.selectTab(util);
//                        break;
//                    case 2:
//                        mTabLayout.selectTab(expand);
//                        break;
//                    default:
//                        break;
//                }
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
//    }

}